/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.josoull.mybatis.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 *
 * @author Renato
 */
@Singleton
@Startup
public class MyBatisConnectionFactory {
    private static SqlSessionFactory sqlSessionFactory;
    
    @PostConstruct
    private void startup() {
        try {

			String resource = "SQLMapConfig.xml";
                        System.out.println(resource);
			Reader reader = Resources.getResourceAsReader(resource);

			if (sqlSessionFactory == null) {
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
			}
		}

		catch (FileNotFoundException fileNotFoundException) {
			fileNotFoundException.printStackTrace();
		}
		catch (IOException iOException) {
			iOException.printStackTrace();
		}
    }
    
    public  SqlSessionFactory getSqlSessionFactory() {

		return sqlSessionFactory;
	}
}
